import { defineStore } from "pinia";

interface IUserState {
    menus: Menu[];
    username: string,
}
interface IMenuType {
    name: string;
    id: number;
    parentId: number;
    title: string;
    icon?: string;
    hidden: number;
    children?: IMenuType[];
}


export const useUserStore = defineStore('user', {
    state: (): IUserState => ({
        menus: [],
        username: ''
    }),
    getters: {
        getMenusRoutes(state) {
            // const newMenus: IMenuType[] = [];
            const newMenus: { [k: number]: IMenuType } = {};

            state.menus.forEach((item) => {
                if (item.parentId == 0) {

                    newMenus[item.id] = { ...item, children: newMenus[item.id]?.children, };
                } else {
                    newMenus[item.parentId] ??= {} as IMenuType;

                    newMenus[item.parentId].children ??= [];
                    newMenus[item.parentId].children!.push(item);
                }
            });
            console.log(newMenus);
            // return newMenus.filter((item) => item);
            return newMenus;
        },
        getPremission(state) {
            return state.menus.map((item) => item.name);
        }

    },
    actions: {
        updateMenus(menus: Menu[]) {
            this.menus = menus;
        }
    }
})


